<?php

class video extends model {
	
	/* @var @metadata videometadata */
	protected $metadata;
	
	// data parsing
	private $entry_nameparse = array("-", " ");
	
	/**
	 * 
	 * @param string $id
	 * @param boolean $autocreate
	 * @return video
	 */
	public static function create($id = null, $autocreate = true, $autoUpdateFromYoutube = false) {
		return new video($id, $autocreate, $autoUpdateFromYoutube);
	}
	
	function __construct($id = null, $autocreate = true, $autoUpdateFromYoutube = false) {
		parent::__construct($id, $autocreate);
		if (!$this->has('name') && $autoUpdateFromYoutube === true) {
			$this->metadata = new videometadata($id);
			$data = $this->updateFromYoutube();
			$this->update($data);
			$this->parse();
			$this->setUpdated();
			$this->addToMusicBrainzCue();
			// hier kwam een loop toen de if constructie niet goed werkte.. runcue maakt namelijk weer nieuwe video aan.. etc..
			musicbrainz::runCueAsync();
		} else {
			$this->metadata = new videometadata($id);
		}
		return $this;
	}
	
	function updateFromYoutube() {
		$yt = new youtube_detail($this->getId());
		return $yt->getData();		
	}
	
	function parse($save = true) {
		$this->addMetaFromParse();
		$this->set("duration", gmdate("i:s", $this->get("duration")));
		if ($save === true) {
			$this->metadata->save();
			$this->save();
		}
	}
	
	/**
	 * 
	 * @return videometadata
	 */
	public function getMetaDataObject() {
		return $this->metadata;
	}
	
	function getMeta($name, $getDynamicMeta = true) {
		if ($name == "artist") {
			return $this->metadata->getArtist($getDynamicMeta);
		} else if ($name == "title") {
			return $this->metadata->getTitle($getDynamicMeta);
		} else {
			return $this->metadata->get($name);
		}
	}
	
	function getMetaArtist($getDynamicMeta = true) {
		return $this->getMeta("artist", $getDynamicMeta);
	}
	
	function getMetaTitle($getDynamicMeta = true) {
		return $this->getMeta("title", $getDynamicMeta);
	}
	
	function save() {
		$values = $this->getAll(false, false, false);
		if (isset($values["id"])) {
			unset($values["id"]);
		}
		return $res = $this->update($values);
	}
	
	/* those might locks magic getter ?? */
	function getAll($includeMeta = false, $getDynamicMeta = true, $addExtendedInfo = true) {
		if ($addExtendedInfo) {
			$this->addExtendedInfo();
		}
		if ($includeMeta) {
			$ret = $this->_data;
			$ret['artist'] = $this->getMetaArtist($getDynamicMeta);
			$ret['title'] = $this->getMetaTitle($getDynamicMeta);
			if ($this->metadata->get("mb_similar") > config::getInstance()->musicbrainz_min_similar) {
				$ret['album'] = $this->getMeta("mb_album");
			}
		} else {
			$ret = $this->_data; 
		}		
		return $ret;
	}
	
	function addMetaFromMusicBrainz() {
		$this->metadata->update(				 
			musicbrainz::queryTrack($this->getName())
		);
		$this->metadata->set("mb_similar", musicbrainz::getSimilarity($this));
		$this->metadata->save();
	}
		
	function addToMusicBrainzCue() {
		musicbrainz::addToCue($this->getId());
	}
	
	function addMetaFromParse() {
		$delimiters = $this->entry_nameparse;
		$arr = array();
		foreach ($delimiters as $delimiter) {
						
			$arr = explode($delimiter, $this->getName(), 2);
			flog($arr);
			if (isset($arr[1])) {
				break;
			}
		}
		if (!isset($arr[1])) {
			$this->metadata->set("parsed_artist", "");
			$this->metadata->set("parsed_title", ucwords(trim($this->getName())));
		} else {
			$this->metadata->set("parsed_artist", ucwords(trim($arr[0])));
			$this->metadata->set("parsed_title", ucwords(trim($arr[1])));
		}
	}
	
	function setUpdated($save = true) {
		if ($save === true) {
			return db::getInstance()->update_sql("UPDATE video SET updatedon = NOW() WHERE id = '".$this->getId()."'");
		} else {
			$this->set("updatedon", date("Y-m-d"));
		}	
	}
	
	function addExtendedInfo() {
		$this->setDownload($this->hasDownload());
		$this->setFav($this->hasFav());		
	}
	
	public function hasFav($user_id = null) {
		$fav = new fav();
		if (is_null($user_id)) {
			$user = module::get("user");
			/* @var $user module_user */		
			if ($user->isAuth()) {				
				return $fav->hasFav($user->getUid(), $this->getId());
			}
		} else {
			$user = new user($user_id);
			return $fav->hasFav($user->getUid(), $this->getId());
		}		
		
	}
	
	public function hasDownload() {
		$download = new download();
		$download->loadByField("video_id", $this->getId());
		return $download->hasId();
	}
	
}

?>